﻿using Sasc.PV.Common;
using Sasc.PV.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace Sasc.PV.BLL
{
    public class CheJianGongXuBase<TCheJian, TGongXuTarget> : ICheJianGongXu<TCheJian, TGongXuTarget>
        where TCheJian : CheJianManagementBase<TGongXuTarget>, new()
        where TGongXuTarget : class, ICheJianGongXuTarget, new()
    {
        #region Ctor
        public CheJianGongXuBase()
        {
            CheJianManagement = new TCheJian();
            GongXuName = CheJianUtils.GetCheJianGongXuName(this.GetType());
        }
        #endregion


        #region Properties
        public string GongXuName { get; private set; }
         
        protected string LoginUserName { get { return PublicMethod.GetSessionValue("UserName"); } }
        public TCheJian CheJianManagement { get; private set; }
        #endregion

        #region Methods
        protected void WriteRiZhi(string logContent)
        {
            //写系统日志
            ERPRiZhi MyRiZhi = new ERPRiZhi();
            MyRiZhi.UserName = LoginUserName;
            MyRiZhi.DoSomething = logContent;
            MyRiZhi.IpStr = Utils.GetIPAddress();
            MyRiZhi.Add();
        }

        protected bool IsFanGongGongXu()
        {
            return typeof(ICheJianFanGongGongXu).IsAssignableFrom(this.GetType());
        }

        public virtual void AddGongXuRec(IPvGongXuModel gongXuModel, TGongXuTarget target)
        {
            using (var transaction = new TransactionScope())
            {
                var gongXuRec = CheJianManagement.AddGongXuRec(this, target);

                gongXuModel.GongXuRecID = gongXuRec.ID;
                gongXuModel.ID = gongXuModel.Add();

                transaction.Complete();
            }

            WriteRiZhi(string.Format("车间：{0} --- 添加工序:{1} 成功，工序记录ID为：{2}.", CheJianManagement.CheJianName, GongXuName, gongXuModel.GongXuRecID));
        }

        public virtual void UpdateGongXuRec(IPvGongXuModel gongXuModel, TGongXuTarget target)
        {
            gongXuModel.Update();

            WriteRiZhi(string.Format("车间：{0} --- 修改工序:{1} 成功，工序记录ID为：{2}.", CheJianManagement.CheJianName, GongXuName, gongXuModel.GongXuRecID));
        }

        public virtual bool DeleteGongXuRecList(string idList)
        {
            using (var transaction = new TransactionScope())
            {
                var gongXuDbTableName = GetGongXuDbTableName();

                var deleteGongXuRecIDWhereSqlBuilder = new StringBuilder();
                deleteGongXuRecIDWhereSqlBuilder.AppendFormat("SELECT {2} FROM {0} WHERE ID IN ({1})", gongXuDbTableName, idList, DbMappingHelper.GetDbFieldName<IPvGongXuModel, long>(c => c.GongXuRecID));
                CheJianManagement.DeleteGongXuRecList(deleteGongXuRecIDWhereSqlBuilder.ToString());

                var deleteGongXuSqlBuilder = new StringBuilder();
                deleteGongXuSqlBuilder.AppendFormat("DELETE FROM {0} WHERE ID IN ({1})", gongXuDbTableName, idList);
                DbHelperSQL.ExecuteSql(deleteGongXuSqlBuilder.ToString());

                transaction.Complete();
            }

            WriteRiZhi(string.Format("车间：{0} --- 删除工序:{1} 成功.", CheJianManagement.CheJianName, GongXuName, idList));
            return true;
        }

        // ToDo:
        // 目前的逻辑：只有当该 target 还未被该工序处理过，
        // 或者已经处理过，但是最新的一次处理后，有其他工序的处理，并且当前是处于返工状态（多次返工）
        public virtual bool CanProcess(IPvGongXuModel gongXuModel, TGongXuTarget target, out string reason)
        {
            return CheJianManagement.CanProcess(this, gongXuModel, target, out reason);
        }
        #endregion

        #region Implement Interface
        public string GetGongXuDbTableName()
        {
            return (string)(DbHelperSQL.GetSingle(string.Format("SELECT GongXuTableName FROM PvcGongXu WHERE Name='{0}' AND CheJianName='{1}'",
                                                                GongXuName,
                                                                CheJianManagement.CheJianName)) ?? default(string));
        }
        #endregion
    }
}
